﻿#include"ActBase.h"

ActBase::ActBase(uint32_t iActType, LogicUser& stUser) : _iActType(iActType), _stUser(stUser)
{
}

ActBase::~ActBase()
{
}

// 刷新玩家活动数据
void ActBase::refreshUsrActData(const ActTableData& stActTableData)
{
	JceData::UserActivity& stUserActivity = _stUser.pActMgr->getUserActivity();
	//查找玩家对应活动数据,没有就添加
	if (stUserActivity.mActData.find(stActTableData.iActId) == stUserActivity.mActData.end())
	{
		JceData::UserActData& stUserActData = stUserActivity.mActData[stActTableData.iActId];
		stUserActData.iActId = stActTableData.iActId;
		stUserActData.iActType = stActTableData.iType;

		_stUser.setChanged();
	}
}

//  获取玩家活动数据
JceData::UserActData* ActBase::getUserActData(const ActTableData& stActTableData)
{
	JceData::UserActivity& stUserActivity = _stUser.pActMgr->getUserActivity();

	std::map<uint32_t, JceData::UserActData>::iterator it = stUserActivity.mActData.find(stActTableData.iActId);
	if (it != stUserActivity.mActData.end())
	{
		return &(it->second);
	}

	return NULL;
}

//解析json字符串中奖励 道具/英雄
int32_t parseReward(const Json::Value& jsonServerParm, map<uint32_t, RewardInfo>& mRewardInfo)
{

		if (jsonServerParm.isMember("progress_conf"))
		{
			const Json::Value& jsonProgressConf = jsonServerParm["progress_conf"];

			for (uint32_t i = 0; i < jsonProgressConf.size(); i++)
			{
				const Json::Value& jsonProgressReward = jsonProgressConf[i];

				uint32_t iProgress = jsonProgressReward["progress"].asUInt();
				if (mRewardInfo.find(iProgress) != mRewardInfo.end())
				{
					return ProtoMsg::Code_Act_ServerParmaFmtError;
				}

				RewardInfo& stRewardInfo = mRewardInfo[iProgress];

				if (jsonProgressReward.isMember("reward_item"))
				{
					string sItemKey = jsonProgressReward["reward_item"].asString();
					bool bRet = TUtil::splitIdNumBatch(sItemKey, ";", ":", stRewardInfo.vItem);
					if (!bRet)
					{
						return ProtoMsg::Code_Act_ServerParmaFmtError;
					}
				}

				if (jsonProgressReward.isMember("reward_hero"))
				{
					string sHeroKey = jsonProgressReward["reward_hero"].asString();
					bool bRet = TUtil::splitHeroKeyBatch(sHeroKey, ";", ":", stRewardInfo.vHero);
					if (!bRet)
					{
						return ProtoMsg::Code_Act_ServerParmaFmtError;
					}
				}
				if (jsonProgressReward.isMember("active"))
				{
					stRewardInfo.iActive = jsonProgressReward["active"].asUInt();
				}
				if (jsonProgressReward.isMember("charge"))
				{
					stRewardInfo.iActive = jsonProgressReward["charge"].asUInt();
				}
			}
		}

	return 0;
}

ActTitleBase::ActTitleBase(uint32_t iActType, LogicUser& stUser) :ActBase(iActType, stUser)
{
}

ActTitleBase::~ActTitleBase()
{

}

bool ActTitleBase::checkCanShow(const ActTableData& stActTableData)
{
	//根据内部子活动 情况,决定最外部活动是否显示
	const ActTableDataVector* vActDatas = NULL;
	if (stActTableData.jsonServerParam.isMember("SubShowType"))
	{
		uint32_t iShowtype = stActTableData.jsonServerParam["SubShowType"].asUInt();
		vActDatas = ACTIVITY_MGR->GetActDataByActShowType(iShowtype);
	}
	else
	{
		vActDatas = ACTIVITY_MGR->GetActDataByActShowType(_iSubActShowType);
	}
	if (vActDatas == NULL)
	{
		return false;
	}

	bool isShow = false;
	ActTableVectorConstIt ActIt = vActDatas->begin();
	for (; ActIt != vActDatas->end(); ActIt++)
	{
		isShow = _stUser.pActMgr->isShowToUser(*ActIt);
		if (isShow)break;
	}
	return isShow;
}

bool ActTitleBase::checkCanJoin(const ActTableData& stActTableData)
{
	//根据内部子活动 情况,决定最外部活动是否可以参加
	const ActTableDataVector* vActDatas = NULL;
	if (stActTableData.jsonServerParam.isMember("SubShowType"))
	{
		uint32_t iShowtype = stActTableData.jsonServerParam["SubShowType"].asUInt();
		vActDatas = ACTIVITY_MGR->GetActDataByActShowType(iShowtype);
	}
	else
	{
		vActDatas = ACTIVITY_MGR->GetActDataByActShowType(_iSubActShowType);
	}
	if (vActDatas == NULL)
	{
		return false;
	}

	bool canJoin = false;
	ActTableVectorConstIt ActIt = vActDatas->begin();
	for (; ActIt != vActDatas->end(); ActIt++)
	{
		canJoin = _stUser.pActMgr->checkCanJoin(*ActIt);
		if (canJoin)break;
	}
	return canJoin;
}

bool ActTitleBase::checkHasReward(const ActTableData& stActTableData)
{
	const ActTableDataVector* vActDatas = NULL;
	//根据内部子活动 检查是否有奖励
	if (stActTableData.jsonServerParam.isMember("SubShowType"))
	{
		uint32_t iShowtype = stActTableData.jsonServerParam["SubShowType"].asUInt();
		vActDatas = ACTIVITY_MGR->GetActDataByActShowType(iShowtype);
	}
	else
	{
		vActDatas = ACTIVITY_MGR->GetActDataByActShowType(_iSubActShowType);
	}

	if (vActDatas == NULL)
	{
		return false;
	}
	//遍历内部子活动 是否有奖励
	bool bHasReward = false;
	ActTableVectorConstIt ActIt = vActDatas->begin();
	for (; ActIt != vActDatas->end(); ActIt++)
	{
		bHasReward = _stUser.pActMgr->checkHasReward(*ActIt);
		//只要一个有奖励,客户端界面就显示小红点
		if (bHasReward)break;
	}
	return bHasReward;
}